<%-- 
    Document   : main
    Created on : 16.04.2013, 12:47:05
    Author     : Yasya, Kate
--%>
<%@page import="entity.Role"%>
<%@page import="entity.Users"%>
<%@page import="java.util.List"%>
<%@page import="facade.ProductFacadeLocal"%>
<%@page import="entity.Category"%>
<%@page import="facade.CategoryFacadeLocal"%>
<%@page import="entity.Product"%>
<%@page import="delta.cart.bean.Cart"%>
<%@page import="delta.cart.bean.CartLocal"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%! private ProductFacadeLocal productFacade;
    private CategoryFacadeLocal categoryFacade;
    private CartLocal cart;
    private Users user;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            productFacade = (ProductFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/ProductFacade!facade.ProductFacadeLocal");
            categoryFacade = (CategoryFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/CategoryFacade!facade.CategoryFacadeLocal");
            cart = (CartLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/Cart!delta.cart.bean.CartLocal");


        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>
<%
    session.setAttribute("cart", cart);
    user = (Users) session.getAttribute("user");
    session.setAttribute("pageNumber", null);
    session.setAttribute("sortBy", null);
    session.setAttribute("pageCount", 10);
%>


<script>
    function cleanName(){
        document.getElementById("name").value="";
        doFilter();
    }    
  
</script>
<script>
    function validateQuantity(id_product){
        var quantity=document.getElementById("quantity_"+id_product);
        if(quantity.value<1){
            alert('Quantity should be grater than 0');  
        }   
    }
    
</script>

<script>
    function displayLoginForm(){
        document.getElementById("login").style.display="block";  
        
    }
   
</script>

<script>
    function addProduct(id_product){
        var xmlhttp;
        var error;
        xmlhttp=new XMLHttpRequest();
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("count_product").value=xmlhttp.responseText.split('|')[0];
                document.getElementById("price").value=xmlhttp.responseText.split('|')[1];
                error=xmlhttp.responseText.split('|')[2];
                if(error!=null){
                    document.getElementById("error").innerHTML=error;
                }else{
                    document.getElementById("error").innerHTML="";   
                }
            }
        }
        var quantity=document.getElementById("quantity_"+id_product).value;
        xmlhttp.open("GET","AddToCart?id_product="+id_product+"&quantity="+quantity,true);
        xmlhttp.send();
    }
</script>
<script>
       
    function getProducts(pageNumber){
       
        var xmlhttp;
        xmlhttp=new XMLHttpRequest();
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("productList").innerHTML=xmlhttp.responseText;
                printPages(pageNumber);
            }
        }
        var name=document.getElementById("name").value;
        var category=document.getElementById("category").value;
        var sortBy=document.getElementById("sortBy").value;
        
        xmlhttp.open("GET",encodeURI("productList.jsp?pageNumber="+pageNumber+
            "&sortBy="+sortBy+
            "&category="+category+
            "&name="+name)
        ,true);
        xmlhttp.send();
    }
</script>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/1.css">
        <link rel="stylesheet" type="text/css" href="css/all.css">
        <script type="text/javascript" src="scripts/jquery-1.8.3.min.js"></script>
        <script type="text/javascript" src="scripts/main.js"></script>
        <script type="text/javascript" src="scripts/paging.js"></script>
        <script type="text/javascript" src="scripts/quantityValidator.js"></script>
        <title>our_product</title>
    </head>

    <body onload="changePage(1);validateTrackingNumber()">
        <header>
            
             <div id ="top_name"> 
                <img src="/Delta-war/product/1.jpeg">
                <h1> <a href="main.jsp">Delta shop</a></h1> 
                <p>Best shopping for you </p>
            </div>

           



            <%if (user != null) {%>
            <div id="registr2">
                <a href="user_name.jsp"><%=user.getLogin()%>  </a>
                <a href="logout">Logout</a></div>
            <div id="user-panel">
                <%if (!user.getRoleCollection().isEmpty()) {
                        for (Role r : user.getRoleCollection()) {
                            if (r.getRoleName().equals("Administrator")) {%>

                <a href="/Delta-war/userview">|Administrator dashboard|</a>
                <%}
                        if (r.getRoleName().equalsIgnoreCase("Warehouse Employee")) {%>
                <a href="/Delta-war/ViewWarehouse">|Warehouse dashboard|</a> 
                <%}
                        if (r.getRoleName().equalsIgnoreCase("Courier")) {%>
                <a href="/Delta-war/courier">|Courier dashboard|</a>
                <% }%>


                <%}
            } else {%>
                <a href="main.jsp">Customer</a><br>
                <%}%>
            </div>
            <%} else {%>
            <div id="registr2">
                <a href="#"
                   onclick="displayLoginForm()"
                   style="position: relative; ">
                    Login 
                </a>
                <a href="registration.jsp"
                   style="position: relative;">
                    Registration
                </a>

                <%}%>
            </div>
 <div id ="shopping">
                <p >  <a href="cart_user.jsp"
                         style="font-size: 12pt;">Cart</a> 
                    <input 
                        id="count_product"
                        type="text" 
                        name="Count_product" 
                        value="<%=cart.getCount()%>"
                        size="1" 
                        readonly /> 
                    products
                </p>
            </div>
            <div id="price_shopping">
                <p> 
                    <input 
                        id="price" 
                        type="text" 
                        name="Prcie" 
                        value="<%=cart.getTotalPrice()%>"
                        size="8" 
                        readonly /> 
                    $ 
                </p> 
            </div>
        </header>

        <div id="main">

            <div id="login" style="display:none; z-index: 1;">
                <form method="POST" action="j_security_check"> 
                    Username: <input type="text" name="j_username" size="30"  id="label"/></br>
                    Password: <input  type="password" name="j_password" size="30" id="label"/></br>
                    <button id="sign" type="submit"  >Sign in</button>
                </form>
            </div>

            <div id="filters" style="margin-top: 15;padding-top: 10;">
                <div style="float: left;margin-left: 30px">
                    <% if (user == null) {%>
                    <form class="find-order">
                        <p>  Find order:
                            <input 
                                id="tr-number" oninput="validateTrackingNumber()"
                                type="text" maxlength="9"
                                name="Find" 
                                size="24" 
                                placeholder="Enter your tracking number" />
                            <button id="go-order" type="submit" name="go" disabled>Go</button> 
                            <span 
                                id="warningFind" 
                                style="color: red; 
                                position: relative;
                                top: 17px;
                                right: 245px;
                                font-size: 12px; ">
                            </span>
                    </form>
                    <%}%>

                    <form method="POST" action="main.jsp"> 
                        <p>Find by name:
                            <input 
                                type="text" 
                                name="Find"  
                                size="21" 
                                value=""  
                                id="name" 
                                placeholder="Enter product name "/>
                            <input style=" width: 50px;  background: #b8b0fd;"
                                id="go"  
                                type="button"   
                                name="go"
                                value="Go"
                                onclick="doFilter()"
                                /> 
                            <input style=" width: 50px;  background: #b8b0fd;
                                id="clean_btn"  
                                type="button"   
                                name="clean"
                                value="Clean"
                                onclick="cleanName()"/> 




                    </form>
                </div>
                <div style="float: left; margin-left: 30px">
                    <form method="GET" id="selectForm" action="main.jsp">
                        <p> Category: 
                            <select  
                                id="category"
                                name="category"  
                                size = 1
                                onchange="doFilter()">
                                <option  
                                    value="-1" selected> all   
                                </option>

                                <%for (Category c : categoryFacade.findAll()) {%>
                                <option 
                                    value=<%=c.getId_category()%> >
                                    <%= c.getName()%> 
                                </option> 
                                <%}%>
                            </select> 
                        </p>
                    </form >                        

                    <form method="GET" id="selectSort"  action="main.jsp" >
                        Sort by:   
                        <select 
                            id="sortBy"
                            name="sortBy"  
                            size = 1 
                            onchange="doFilter()"
                            style="width:100"> 
                            <option value="1">price asc</option>
                            <option value="2">price desc</option>
                            <option value="3">name desc</option>
                            <option value="4" selected>name asc</option>            
                        </select> 
                    </form>
                </div>
                <div style="clear:left"></div>
            </div> 
            <hr>

            <div id="top_pages"  >

            </div>
            <center>
                <font color="red">   
                <span 
                    id="error"> 
                </span>
                </font>
            </center>

            <div id="productList">
            </div>
        <hr>
                  <div id="bottom_pages"  >

            </div>
            
        </div>
                            <div id="footer">
                                <p>  &#169;All rights reserved</p>
                            </div>
    </body>
</html>
